//Ngo Anh Duong
#define SQL_CLIENT
using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace Xmobile.Users
{


	public class MainDB : MainDB_Base
	{
		public MainDB()
		{
			// EMPTY
		}
		public DataTable SelectQuery(string sql)
		{
			IDbCommand cmd=this.CreateCommand(sql,false);
			return this.CreateDataTable(cmd);
		}
		public void AnotherNonQuery(string sql)
		{
			IDbCommand cmd=this.CreateCommand(sql,false);
			cmd.ExecuteNonQuery();
		}
		public DataTable CallStoredProcedure(string nameOfStored,bool returnDataTable)
		{
			IDbCommand cmd=this.CreateCommand(nameOfStored,true);			
			if(returnDataTable)
			{
				return this.CreateDataTable(cmd);
			}
			else
			{
				cmd.ExecuteNonQuery();
				return null;
			}
		}

		public DataTable CallStoredProcedure(ArrayList listOfPara,string nameOfStored,bool returnDataTable)
		{
			IDbCommand cmd=this.CreateCommand(nameOfStored,true);			
			for(int i=0;i<listOfPara.Count;i++)
			{
				SqlParameter sp=(SqlParameter)listOfPara[i];
				AddParameter(cmd,sp.ParameterName,sp.DbType,sp.Value);
			}
			if(returnDataTable)
			{
				return this.CreateDataTable(cmd);
			}
			else
			{
				cmd.ExecuteNonQuery();
				return null;
			}						
		}
		protected override IDbConnection CreateConnection()
		{
            return new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Users"].ToString());
		}
		protected internal DataTable CreateDataTable(IDbCommand command)
		{
			DataTable dataTable = new DataTable();
#if ODBC
			new System.Data.Odbc.OdbcDataAdapter((System.Data.Odbc.OdbcCommand)command).Fill(dataTable);
#elif SQL_CLIENT
			new System.Data.SqlClient.SqlDataAdapter((System.Data.SqlClient.SqlCommand)command).Fill(dataTable);
#else
			new System.Data.OleDb.OleDbDataAdapter((System.Data.OleDb.OleDbCommand)command).Fill(dataTable);
#endif		
			return dataTable;
		}
		
		//bacth [3:07 PM 7/21/2008]
		public DataTable CallStoredProcedure(string nameOfStored, object[] parValues, string[] parNames, bool returnDataTable)
		{
			IDbCommand cmd = this.CreateCommand(nameOfStored, true);
			for (int i = 0; i < parValues.Length; i++)
			{
				cmd.Parameters.Add(new SqlParameter(parNames.GetValue(i).ToString(), parValues.GetValue(i)));
			}
			if (returnDataTable)
			{
				return this.CreateDataTable(cmd);
			}
			else
			{
				cmd.ExecuteNonQuery();
				return null;
			}
		}

		protected internal override string CreateSqlParameterName(string paramName)
		{
#if ODBC
			return "?";
#elif SQL_CLIENT
			return "@" + paramName;
#else
			return "?";
#endif
		}
		protected override string CreateCollectionParameterName(string baseParamName)
		{
#if ODBC
			return "@" + baseParamName;
#else
			return "@" + baseParamName;
#endif
		}
	}
}

